<?php
class District_model extends Model{

	var $_datagrid_name = 'dt1';	
	
    function District_model(){
        parent::Model();       
    }
    function getdatasource($id){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('realestate_district');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('realestate_district.id,realestate_district.name,realestate_province.name,realestate_district.sortorder');
		$this->db->join('`realestate_province','provinceid = realestate_province.id');
		if($id!=''){ $this->db->where('provinceid', $id); }
    	$query = $this->db->get('realestate_district');
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
		//$count_ = count($query->result());
		//echo $count_;
		//$datagrid->pages = intval($count_/$limit) + ($count_%$limit?1:0);
    	$datagrid->datasource = $query->result_rowlist();
    	return $datagrid;
    }
	// get province
	function getprovince()
	{
		$this->db->order_by('sortorder');
		$query = $this->db->get('realestate_province');
		if($query->num_rows())
		{
			return $query->result();
		}
		else return false;
	}
    function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$query = $this->db->get_where('realestate_district',array('id'=>$id));
    		if($query->num_rows()){    					
    			$this->db->delete('realestate_district',array('id'=>$id));
    		}
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
	    		$query = $this->db->get_where('realestate_district',array('id'=>$id[$i]));
	    		if($query->num_rows()){    					
	    			$this->db->delete('realestate_district',array('id'=>$id[$i]));
	    		}
    		}
    	}
    }
    function getnewprofile(){
		$res->username = $this->input->post('txt_username');
		$res->firstname = $this->input->post('txt_firstname');
		$res->lastname = $this->input->post('txt_lastname');
		$res->phone = $this->input->post('txt_phone');
		$res->email = $this->input->post('txt_email');
		$res->alertmsg = empty($res->username)?"":lang('usermanagement_msg_invalidusername');
		return $res;
	}
	function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
			$res = $this->db->get_where('realestate_district',array('name'=>$this->input->post('txt_district')));
			if($res->num_rows) return FALSE;
    		$this->db->insert('realestate_district',array('name'=>$this->input->post('txt_district'),										
											'provinceid'=>$this->input->post('txt_provinceid')));
    		redirect('list_district','location');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$this->db->where('id',$this->input->post('hidden_id'));			
			if($this->input->post('txt_district')!=''){
				$this->db->update('realestate_district',array('name'=>$this->input->post('txt_district'),
															  'provinceid'=>$this->input->post('txt_provinceid')));
			}
			redirect('list_district','location');
    	}
    }
   	function getuserprofile($id){
    	$query = $this->db->get_where('realestate_district',array('id'=>$id));
    	if($query->num_rows){
    		return $query->row();
    	}
    	else return FALSE;
    }
   function save(){
		if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		$sortorder = $this->input->post($this->_datagrid_name.'_column_3');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('realestate_district',array('sortorder'=>$sortorder[$i]));
    		}    		
    	}		
    }
}

/* End of file yourprofile_model.php */
/* Location: ./system/application/admin/controllers/yourprofile_model.php */